Telegram Group & Telegram Channel
Наверное, самая распространённая ошибка новичков в Python — это использование изменяемого объекта в качестве значения по умолчанию для аргумента функции. Такой объект будет общим для всех вызовов функции, что может привести к неожиданным результатам:


def append_length(lst=[]):
lst.append(len(lst))
return lst

print(append_length([1, 2])) # [1, 2, 2]
print(append_length()) # [0]
print(append_length()) # [0, 1]


Однако в некоторых случаях, например при реализации кешей, такое поведение может быть полезным:


def fact(x, cache={0: 1}):
if x not in cache:
cache[x] = x * fact(x - 1)
return cache[x]

print(fact(5))


В этом примере мы сохраняем вычисленные значения факториала внутри значения аргумента по умолчанию. Причём к этому значению даже можно обратиться напрямую:


>>> fact.__defaults__
({0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120},)


👉@BookPython



tg-me.com/BookPython/3670
Create:
Last Update:

Наверное, самая распространённая ошибка новичков в Python — это использование изменяемого объекта в качестве значения по умолчанию для аргумента функции. Такой объект будет общим для всех вызовов функции, что может привести к неожиданным результатам:


def append_length(lst=[]):
lst.append(len(lst))
return lst

print(append_length([1, 2])) # [1, 2, 2]
print(append_length()) # [0]
print(append_length()) # [0, 1]


Однако в некоторых случаях, например при реализации кешей, такое поведение может быть полезным:


def fact(x, cache={0: 1}):
if x not in cache:
cache[x] = x * fact(x - 1)
return cache[x]

print(fact(5))


В этом примере мы сохраняем вычисленные значения факториала внутри значения аргумента по умолчанию. Причём к этому значению даже можно обратиться напрямую:


>>> fact.__defaults__
({0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120},)


👉@BookPython

BY Библиотека Python разработчика | Книги по питону


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/BookPython/3670

View MORE
Open in Telegram


Библиотека Python разработчика | Книги по питону Telegram | DID YOU KNOW?

Date: |

Telegram Be The Next Best SPAC

I have no inside knowledge of a potential stock listing of the popular anti-Whatsapp messaging app, Telegram. But I know this much, judging by most people I talk to, especially crypto investors, if Telegram ever went public, people would gobble it up. I know I would. I’m waiting for it. So is Sergei Sergienko, who claims he owns $800,000 of Telegram’s pre-initial coin offering (ICO) tokens. “If Telegram does a SPAC IPO, there would be demand for this issue. It would probably outstrip the interest we saw during the ICO. Why? Because as of right now Telegram looks like a liberal application that can accept anyone - right after WhatsApp and others have turn on the censorship,” he says.

The seemingly negative pandemic effects and resource/product shortages are encouraging and allowing organizations to innovate and change.The news of cash-rich organizations getting ready for the post-Covid growth economy is a sign of more than capital spending plans. Cash provides a cushion for risk-taking and a tool for growth.

Библиотека Python разработчика | Книги по питону from br


Telegram Библиотека Python разработчика | Книги по питону
FROM USA